<template>
  <a-modal
    :title="title"
    :width="modelWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <div class="table-page-search-wrapper">
      <a-form-model layout="inline" :model="queryParam">
        <a-row :gutter="10">
          <a-col :md="8" :sm="12" :offset="6">
            <a-form-model-item label="" prop="realname" style="margin-left: 8px">
              <a-input placeholder="请输入账套名称" v-model="queryParam.keyWord"></a-input>
            </a-form-model-item>
          </a-col>
          <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="12">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </div>
    <a-spin :spinning="confirmLoading">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="isPagination ? ipagination : false"
        :customRow="handleClickRow"
        @expand="expandRecord"
        :expanded-row-keys.sync="expandedRowKeys"
        @change="handleTableChange"
        style="height: 500px; overflow-x: hidden; overflow-y: auto"
      >
      </a-table>
    </a-spin>
  </a-modal>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { Treecolumns, columns2, columns8, columns10, columns11, columns36 } from './modalColumns'
export default {
  name: 'ProjectModel',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      title: '辅助',
      visible: false,
      disableSubmit: false,
      isCheck: false,
      confirmLoading: false,
      columns: [],
      url: {
        list: '',
        searchBy: '/sjyzzx/sjZxzyAssistAccount/searchBy',
      },
      queryParam: {
        type: '0',
        keyWord: '',
      },
      expandedRowKeys: [],
      disableMixinCreated: true,
      loading: false,
      modelWidth: 800,
      isPagination: false,
    }
  },
  created() {},
  watch: {},
  methods: {
    searchReset() {
      this.queryParam.keyWord = ''
      // 重置之后恢复表头
      this.getColumns(this.queryParam.type)
      this.loadData(1)
    },
    searchQuery() {
      this.loading = true
      // 查询时用树的表
      this.getColumns(this.queryParam.type)
      getAction(this.url.searchBy, this.queryParam).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    edit(record) {
      debugger
      this.url.list = record.interfaceName
      this.queryParam.type = record.assistType
      // 根据类型选择不一样的表头
      this.dataSource = []
      this.getColumns(record.assistType)
      this.loadData(1)
      this.visible = true
    },
    handleData(data) {
      if (data.length) {
        data.map((item) => {
          if (item.children&&item.children.length == 0) {
            delete item.children
          }
        })
      }
      return data
    },
    // 处理数据children数据
    expandRecord(expanded, record) {
      if (expanded) {
          // debugger
          record.children.map((item) => {
            if (item.children !== undefined && item.children.length == 0) delete item.children
          })
      }
    },
    loadData(arg) {
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() // 查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          // update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          let data = res.result.records || res.result
          this.dataSource = this.handleData(data)
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          // update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    getColumns(type) {
      // 需要分页
      this.isPagination = true

      switch (type) {
        case '02':
          this.columns = columns2(this)
          this.modelWidth = 1100
          break
        case '08':
          this.columns = columns8(this)
          this.modelWidth = 1200
          break
        case '10':
          this.columns = columns10(this)
          this.modelWidth = 800
          break
        case '11':
          this.columns = columns11(this)
          this.modelWidth = 1300
          break
        case '36':
          this.columns = columns36(this)
          this.modelWidth = 1100
          break
        default:
          this.columns = Treecolumns(this)
          this.isPagination = false
          this.modelWidth = 800
      }
    },
    handleClickRow(record, index) {
      return {
        on: {
          // 双击
          dblclick: () => {
            console.log(record)
            this.$emit('onSelect', record)
            this.visible = false
            // console.log(8,this.checkedRows)
            // console.log(9,record.id)
            // console.log(record, index)
            // console.log('点击行内容record' + record)
            // console.log('序号索引index' + index)
          },
        },
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.$emit('ok', this.model)
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    expandIcon(props) {
      console.log(99994444, props)
    },
  },
}
</script>

<style scoped>
</style>
<style lang="less" scoped>
.table-bordered /deep/ .ant-col-sm-16 {
  width: 100%;
}
.right {
  .main {
    padding: 10px;
    background-color: #f1f5ff;
  }
}
.box {
  padding: 20px;
}
</style>